package cn.com.clearbus.mini.service.impl;


import cn.com.clearbus.mini.utils.MaiUtils;
import cn.com.clearbus.mini.entity.MailBean;
import cn.com.clearbus.mini.entity.MiniUser;
import cn.com.clearbus.mini.mapper.MiniUserMapper;
import cn.com.clearbus.mini.service.IMiniUserService;
import cn.com.clearbus.result.DynamicResult;
import cn.com.clearbus.result.ErrCode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuwz
 * @since 2019-10-14
 */
@Service
public class MiniUserServiceImpl extends ServiceImpl<MiniUserMapper, MiniUser> implements IMiniUserService {

    private Logger logger = LoggerFactory.getLogger(MiniUserServiceImpl.class);

    @Autowired
    MaiUtils maiUtils;


    @Override
    public MiniUser isMember(MiniUser miniUser) {
        MiniUser miniUserByOpenId = baseMapper.getMiniUserByOpenId(miniUser.getOpenId());
        if (miniUserByOpenId == null) {
            MiniUser miniUser1 = new MiniUser();
            MiniUser no = miniUser1.setUsername("未找到");
            return no;
        }
        return miniUserByOpenId;
    }


    @Override
    @Transactional
    public DynamicResult saveMember(MiniUser miniUser) {
        //测试是否可发送邮件信息
        MailBean mailBean = new MailBean();
        mailBean.setSubject("测试发送邮件").setContent("123456789")
                .setRecipient(new String[]{miniUser.getEmail()});
        if (maiUtils.sendSimpleMail(mailBean) == 0) {
            logger.info("邮件发送失败");
            return new DynamicResult<>(ErrCode.FAILURE, "邮件不可使用");
        }
        if (save(miniUser)) {
            return new DynamicResult(ErrCode.SUCCESS,"保存成功");
        }
        return   new DynamicResult(ErrCode.SUCCESS,"查询失败");
    }



    @Override
    public DynamicResult qrcode(String qrcode) {
        //TODO 获取qrcode对应的用户的openId
        String openId = "123456";
        //通过openId查询数据库是否存在对应的
        MiniUser miniUserByOpenId = baseMapper.getMiniUserByOpenId(openId);
        if (miniUserByOpenId == null) {
            return new DynamicResult(ErrCode.FAILURE,"查询失败");
        }
        return new DynamicResult(ErrCode.SUCCESS,"查询成功");
    }

    @Override
    public List<MiniUser> getAllMiniUser() {
        return baseMapper.getAllMiniUser();
    }
}
